package com.hangsu.train.components.web.controller;

import com.hangsu.train.components.convert.RoleConvert;
import com.hangsu.train.components.entity.Permiss;
import com.hangsu.train.components.entity.Role;
import com.hangsu.train.components.params.CodeParam;
import com.hangsu.train.components.params.RoleAddParam;
import com.hangsu.train.components.params.RolePermissParam;
import com.hangsu.train.components.params.RoleSearchParam;
import com.hangsu.train.components.service.PermissService;
import com.hangsu.train.components.service.RoleService;
import com.hangsu.train.components.vo.RolePermissVo;
import com.hangsu.train.core.result.Resp;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lyy
 * @Date 2021/12/2
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    private RoleService roleService;

    private PermissService permissService;

    private RoleConvert roleConvert;

    @Resource
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @Resource
    public void setRoleConvert(RoleConvert roleConvert) {
        this.roleConvert = roleConvert;
    }

    @Resource
    public void setPermissService(PermissService permissService) {
        this.permissService = permissService;
    }

    /**
     * 增加角色
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/create")
    public Resp createRole(@RequestBody @Valid RoleAddParam param) {
        Role role = roleConvert.convert(param);
        roleService.addRole(role);
        return Resp.ok();
    }

    /**
     * 删除角色
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/del")
    public Resp delRole(@RequestBody @Valid CodeParam param) {
        roleService.delRole(param.getCode());
        return Resp.ok();
    }

    /**
     * 角色列表
     *
     * @return
     */
    @PostMapping(value = "/list")
    public Resp list(RoleSearchParam param) {
        Page<Role> page = roleService.list(param);
        return Resp.ok(page);
    }


    @PostMapping("/permiss")
    public Resp<RolePermissVo> getPermiss(@RequestBody @Valid CodeParam param) {
        RolePermissVo rp = new RolePermissVo();
        rp.setPermissList(permissService.getAllPermiss());
        List<Permiss> permissList = permissService.getRolePermiss(param.getCode());
        List<String> ids =permissList.stream().map(item->String.valueOf(item.getId())).toList();
        rp.setKeys(ids);
        return Resp.ok(rp);
    }

    /**
     * 修改角色权限
     *
     * @param reqDto
     * @return
     */
    @PostMapping(value = "/authz")
    public Resp authz(@RequestBody @Valid RolePermissParam reqDto) {
        roleService.authz(reqDto);
        return Resp.ok();
    }

}